if (!jsx3.lang.Class.forName("ux.cdb.admin.approvePkg.ApprovePkg")) {

/**
 * Define the <code>name of class</code> class.
 * This class inherit from <code>jsx3.gui.Block<code> GUI class.
 */
jsx3.Class.defineClass(
		"ux.cdb.admin.approvePkg.ApprovePkg",
		jsx3.gui.Block,
		null,
		function (objRoot) {
			/**
			* tool functions:
			*/
			objRoot.prototype.property = function(name, token) {
				return ux.megatron.Main.getServer().getDynamicProperty(name, token);
			};

			objRoot.prototype.onAfterDeserialize = function() {
				this.isError = false;
				this.getPrivilege();
				if(this.isError){
					this.getDescendantOfName("blkApprovelDetail").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
					return;
				}else{
					this.blkApvPkgDesc = this.get("blkApvPkgDesc");
					this.mtxApvPkg = this.get("mtxApvPkg");
					this.getDescendantOfName('labelName1').setText(ux.megatron.Main.getServer().getDynamicProperty("@cdb@approvePkg@approvePkgReview"), true);
					this.curPage = ux.megatron.Main.getServer().getDynamicProperty("@cdb@apvPkg@curPage");
					this.pageSize = ux.megatron.Main.getServer().getDynamicProperty("@cdb@apvPkg@pageNum");
					this.pagebar = this.get("blkPageWheel");
					this.pagebar.initialize();
					this.pagebar.bindWithMatrix(this, this.loadPagination, this.mtxApvPkg);
					this.setStatusAfterGetRole();
				}
			};

			objRoot.prototype.getPrivilege = function() {
				var  privilegebtnNew = ux.cdb.admin.Privilege.getPrivilege(34);
				var  privilegebtnCancelBatch = ux.cdb.admin.Privilege.getPrivilege(35);
				var  privilegebtnReview = ux.cdb.admin.Privilege.getPrivilege(36);
				var  privilegebtnExam = ux.cdb.admin.Privilege.getPrivilege(37);
				var  privilegebtnConfirm = ux.cdb.admin.Privilege.getPrivilege(38);

				if(privilegebtnNew == null){
					this.get("btnNew").hide();
				}else{
					this.get("btnNew").enable();
				}

				if(privilegebtnCancelBatch == null){
					this.get("btnCancelBatch").hide();
				}else{
					this.get("btnCancelBatch").disable();
				}

				if(privilegebtnReview == null){
					this.get("btnReview").hide();
				}else{
					this.approvalRole = 36;
					this.get("btnReview").disable();
				}

				if(privilegebtnExam == null){
					this.get("btnExam").hide();
				}else{
					this.approvalRole = 37;
					this.get("btnExam").disable();
				}

				if(privilegebtnConfirm == null){
					this.get("btnConfirm").hide();
				}else{
					this.approvalRole = 38;
					this.get("btnConfirm").disable();
				}

				if(privilegebtnReview != null && (privilegebtnExam != null || privilegebtnConfirm != null)){
					alert(ux.megatron.Main.getServer().getDynamicProperty("@cdb@apvPkg@permissionErrorInfo"));
					this.isError = true;
				}
            };

			objRoot.prototype.loadPagination = function(msg) {
                this.curPage = (msg && msg.currentPageNumber) ? msg.currentPageNumber : (this.curPage || 1);
                this.loadApvPkgData();
            };

			objRoot.prototype.setStatusAfterGetRole = function(){
				this.blkApvPkgDesc.show();
				var me = this;
				this.mtxApvPkg.subscribe(ux.cdb.admin.gui.Matrix.EVENT_CHECKALL,me,me.onCheckBtnStatus);
				this.loadFilterList();
				this.loadApvPkgData();
			};

			objRoot.prototype.loadDetailTabPane = function(){
				var me = this;
				if(!this.get("tabbedpaneDetail")){
					if(this.isMainMode){
						ux.megatron.Main.loadUIResourceAsync(this.getDescendantOfName('paneDetail'),
								"ux.cdb.admin", 
								"ux/cdb/admin/approvePkg/ui/apvPkgDetail.xml",
								me,me.onLoadModeListTabUI);
					}else{
						ux.megatron.Main.loadUIResourceAsync(this.getDescendantOfName('paneDetail'),
								"ux.cdb.admin", 
								"ux/cdb/admin/approvePkg/ui/apvPkgDetail.xml",
								me,me.onLoadNotModeListTabUI);
					}
				}else{
					if(this.isMainMode){
						this.onLoadModeListTabUI();
					}else{
						this.onLoadNotModeListTabUI();
					}
					this.onLoadCommentInfoTabUI();
				}
			};

			objRoot.prototype.onLoadNotModeListTabUI = function(){
				if(this.get("tabComment").selected){
					this.get("tabRcpList").select();
				}
				var me = this;
				var parent = this.getDescendantOfName('tabRcpList').getContentPane();
				if(parent.getChildren().length == 0){
					ux.megatron.Main.loadUIResourceAsync(parent,
								"ux.cdb.admin", 
								"ux/cdb/admin/approvePkg/ui/rcpPkgList.xml",
								me,me.onLoadNotModeListSuccess);
				}else{
					var parentId = "test...."
					//need Paul's function
					//this.notModePaneObject.loadProvRcpPkgData(parentId);
				}
			};

			objRoot.prototype.onLoadNotModeListSuccess = function(objEvent){
				this.loadUIojb = objEvent.obj;
				this.notModePaneObject = objEvent.obj;
				var parentId = this.mtxApvPkg.getSelectedNodes().get(0).getAttribute("jsxid");
				this.notModePaneObject.initUI(parentId);
			};

			objRoot.prototype.onLoadModeListTabUI = function(){
				if(this.get("tabComment").selected){
					this.get("tabRcpList").select();
				}
				var me = this;
				var parent = this.getDescendantOfName('tabRcpList').getContentPane();
				if(parent.getChildren().length == 0){
					ux.megatron.Main.loadUIResourceAsync(parent,
							"ux.cdb.admin", 
							"ux/cdb/admin/approvePkg/ui/provPkgList.xml",
							me,me.onLoadModeListSuccess);
				}else{
					var parentId = this.mtxApvPkg.getSelectedNodes().get(0).getAttribute("jsxid");
					this.modePaneObject.initUI(parentId);
				}
			};

			objRoot.prototype.onLoadModeListSuccess = function(objEvent){
				this.modePaneObject = objEvent.obj;
				var parentId = this.mtxApvPkg.getSelectedNodes().get(0).getAttribute("jsxid");
				this.modePaneObject.initUI(parentId);
			};

			objRoot.prototype.onLoadCommentInfoTabUI = function(){
				var parent = this.getDescendantOfName('tabComment').getContentPane();
				if(parent.getChildren().length == 0){
					var me = this;
					ux.megatron.Main.loadUIResourceAsync(this.getDescendantOfName('tabComment').getContentPane(),
							"ux.cdb.admin", 
							"ux/cdb/admin/approvePkg/ui/commentInfo.xml",
							me,me.setDataToCommentTabPane);
				}else{
					this.setDataToCommentTabPaneLoad();
				}
			};

			objRoot.prototype.setDataToCommentTabPane = function(objLoad){
				this.reviewer = objLoad.obj;
				this.setDataToCommentTabPaneLoad();
			};

			objRoot.prototype.setDataToCommentTabPaneLoad = function(){
				if(this.approvalRole == 36){
					this.reviewer.getDescendantOfName('paneReview').setDisplay(jsx3.gui.Block.DISPLAYNONE,true);
					this.reviewer.getDescendantOfName('paneExam').setDisplay(jsx3.gui.Block.DISPLAYNONE,true);
					this.reviewer.getDescendantOfName('paneConfirm').setDisplay(jsx3.gui.Block.DISPLAYNONE,true);
				}else if(this.approvalRole == 37){
					this.reviewer.getDescendantOfName('paneReview').setDisplay(jsx3.gui.Block.DISPLAYBLOCK,true);
					this.reviewer.getDescendantOfName('paneExam').setDisplay(jsx3.gui.Block.DISPLAYNONE,true);
					this.reviewer.getDescendantOfName('paneConfirm').setDisplay(jsx3.gui.Block.DISPLAYNONE,true);
					this.loadApprovalData();
				}else if(this.approvalRole == 38){
					this.reviewer.getDescendantOfName('paneReview').setDisplay(jsx3.gui.Block.DISPLAYBLOCK,true);
					this.reviewer.getDescendantOfName('paneExam').setDisplay(jsx3.gui.Block.DISPLAYBLOCK,true);
					this.reviewer.getDescendantOfName('paneConfirm').setDisplay(jsx3.gui.Block.DISPLAYNONE,true);
					this.loadApprovalData();
				}
			};
			
			objRoot.prototype.loadApprovalData = function(){
				var me = this;
				ux.megatron.util.Dwr.callDwr("RetailStudentLoanBatchExamUCC",
						"getOpinionByVirtualApproveObjectId",[this.mtxApvPkg.getSelectedNodes().get(0).getAttribute("virtualApproveObjectId")],
						me,me.callBackgetOpinionByVirtualApproveObjectId,
						"RetailStudentLoanBatchExamUCC");
			};

			objRoot.prototype.callBackgetOpinionByVirtualApproveObjectId = function(dwrObjectList){
				if(!dwrObjectList){
					jsx3.log("DWR getOpinionByVirtualApproveObjectId failed");
					return;
				}
				ux.megatron.Main.loadResource("ux/cdb/admin/approvePkg/docs/commentData.xml",
							"ux.cdb.admin", "commentData_0304_2", "xml");
				var commentData = ux.megatron.Main.getServer().getCache().getDocument("commentData_0304_2");
				if(!dwrObjectList.content.data[0].approveConclusionInfoList || dwrObjectList.content.data[0].approveConclusionInfoList.length == 0){
					commentData.getChildNodes().get(0).setAttribute("reviewSummary","");
					commentData.getChildNodes().get(0).setAttribute("reviewIdea","");
					commentData.getChildNodes().get(0).setAttribute("reviewer","");
					commentData.getChildNodes().get(0).setAttribute("reviewDate","");
					commentData.getChildNodes().get(0).setAttribute("examSummary","");
					commentData.getChildNodes().get(0).setAttribute("examIdea","");
					commentData.getChildNodes().get(0).setAttribute("examer","");
					commentData.getChildNodes().get(0).setAttribute("examDate","");
				}else{ 
					if(this.approvalRole == 37){
						//ReviewInfo
						var reviewInfo = dwrObjectList.content.data[0].approveConclusionInfoList[0];	
						if(reviewInfo.conclusionCode == "01"){
							commentData.getChildNodes().get(0).setAttribute("reviewSummary","同意");
						}else{
							commentData.getChildNodes().get(0).setAttribute("reviewSummary","不同意");
						}
						var month = reviewInfo.conclusionIssueDatetime.getMonth() + 1;
						var reviewDate = reviewInfo.conclusionIssueDatetime.getYear() + "-" + month + "-" + reviewInfo.conclusionIssueDatetime.getDate();
						commentData.getChildNodes().get(0).setAttribute("reviewIdea",reviewInfo.conclusionDesc);
						commentData.getChildNodes().get(0).setAttribute("reviewer",reviewInfo.sysUserName);
						commentData.getChildNodes().get(0).setAttribute("reviewDate",reviewDate);
					}else if(this.approvalRole == 38){
						//ReviewInfo
						var reviewInfo = dwrObjectList.content.data[0].approveConclusionInfoList[0];	
						if(reviewInfo.conclusionCode == "01"){
							commentData.getChildNodes().get(0).setAttribute("reviewSummary","同意");
						}else{
							commentData.getChildNodes().get(0).setAttribute("reviewSummary","不同意");
						}
						var month = reviewInfo.conclusionIssueDatetime.getMonth() + 1;
						var reviewDate = reviewInfo.conclusionIssueDatetime.getYear() + "-" + month + "-" + reviewInfo.conclusionIssueDatetime.getDate();
						commentData.getChildNodes().get(0).setAttribute("reviewIdea",reviewInfo.conclusionDesc);
						commentData.getChildNodes().get(0).setAttribute("reviewer",reviewInfo.sysUserName);
						commentData.getChildNodes().get(0).setAttribute("reviewDate",reviewDate);
						
						//ExamInfo
						var examInfo = dwrObjectList.content.data[0].approveConclusionInfoList[1];
						if(examInfo.conclusionCode == "01"){
							commentData.getChildNodes().get(0).setAttribute("examSummary","同意");
						}else{
							commentData.getChildNodes().get(0).setAttribute("examSummary","不同意");
						}
						var month = examInfo.conclusionIssueDatetime.getMonth() + 1;
						var examDate = examInfo.conclusionIssueDatetime.getYear() + "-" + month + "-" + examInfo.conclusionIssueDatetime.getDate();
						commentData.getChildNodes().get(0).setAttribute("examIdea",examInfo.conclusionDesc);
						commentData.getChildNodes().get(0).setAttribute("examer",examInfo.sysUserName);
						commentData.getChildNodes().get(0).setAttribute("examDate",examDate);
					}
				}
				this.get("blkCommentInfo").setData(commentData);
				this.get("blkCommentInfo").repaint();
			};

			objRoot.prototype.loadFilterList = function(){
				ux.megatron.Main.loadResource("ux/cdb/admin/approvePkg/docs/apvStatusFilter.xml", 
						"ux.cdb.admin", "apvPkgFilter_0304_1", "xml");
				this.get("selApvStatusFilter").repaint();
			};

			objRoot.prototype.loadApvPkgData = function(){
				var me = this; 
				ux.megatron.util.Dwr.callDwr("RetailStudentLoanBatchAuditingUCC",
						"getConcentrateApprovalInfoList",[this.approvalRole,this.curPage,this.pageSize],
						me,me.callBackgetConcentrateApprovalInfoList,
						"RetailStudentLoanBatchAuditingUCC");
			};

			objRoot.prototype.callBackgetConcentrateApprovalInfoList = function(dwrObjectList){
				if(!dwrObjectList){
					jsx3.log("DWR getConcentrateApprovalInfoList failed");
					return;
				}
				this.totalRecordCount = dwrObjectList.content.page.count;
				this.totalPages = (this.totalRecordCount % this.pageSize > 0) ? (parseInt(this.totalRecordCount / this.pageSize) + 1) : (this.totalRecordCount / this.pageSize);
				if (this.totalPages == 0) this.totalPages = 1;
				this.pagebar.setValue(this.curPage,this.totalPages,this.pageSize,this.totalRecordCount); 

				var cdfDoc = ux.megatron.util.Dwr.toCdfDocument(dwrObjectList.content.data,"jsxroot",{"concentrateApproveInfoId":"jsxid"});
				if(dwrObjectList.content.data.length != 0){
					if(dwrObjectList.content.data[0].branchApproveMode == "01"){
						this.isMainMode = true;
					}
					else if(dwrObjectList.content.data[0].branchApproveMode == "02"){
						this.isMainMode = false;
					}
				}
				this.mtxApvPkg.setSourceXML(cdfDoc);
				this.mtxApvPkg.repaintData();
			};

			objRoot.prototype.onChooseApprovePkg = function(){
				if(this.blkApvPkgDesc.getDisplay() != jsx3.gui.Block.DISPLAYNONE){
					this.blkApvPkgDesc.setDisplay(jsx3.gui.Block.DISPLAYNONE,true);
				}
				this.loadDetailTabPane();
			};

			objRoot.prototype.onNewApvPkg = function(){
				var parent = ux.megatron.Main.getServer().getBodyBlock();
				var me = this;
				if(this.isMainMode){
					ux.megatron.Main.loadUIResourceAsync(parent,
						"ux.cdb.admin",
						"ux/cdb/admin/approvePkg/provPkgWizard/ui/apvModePkgBuilderDailog.xml",
						me, me.apvModePkgBuilderDailogLoaded);					
				}else{
					ux.megatron.Main.loadUIResourceAsync(parent,
						"ux.cdb.admin",
						"ux/cdb/admin/approvePkg/pkgWizard/ui/apvPkgBuilderDailog.xml",
						me, me.apvPkgBuilderDailogLoaded);		
				}
			};

			objRoot.prototype.apvModePkgBuilderDailogLoaded = function(objEvent){ 
				this.checkDialog = objEvent.obj;
				this.checkDialog.opener = this; 
			};

			objRoot.prototype.apvPkgBuilderDailogLoaded = function(objEvent){ 
				this.checkDialog = objEvent.obj;
				this.checkDialog.opener = this; 
			};

			objRoot.prototype.onEditReviewApvPkg = function(){
				var parent = ux.megatron.Main.getServer().getBodyBlock();
				var me = this;
				ux.megatron.Main.loadUIResource(parent,"ux.cdb.admin","ux/cdb/admin/approvePkg/ui/reviewDialog.xml",true);
			};

			objRoot.prototype.onSubmitApvPkg = function(){
				var me = this;
				var tipTitle = this.property("@cdb@apvPkg@submitReview");
				var batchId = "BA12345";
				var tipContent = this.property("@cdb@apvPkg@submitContent",batchId);
					//Desc : public Res submitOpinion(ApproveConclusionInfo ApproveConclusionInfo,String approvalProcessInfoId,String concentrateApprovalInfoId)
				ux.megatron.Dialog.confirm(tipTitle, tipContent, function(dialog){
						ux.megatron.util.Dwr.callDwr("RetailStudentLoanBatchExamUCC",
							"submitOpinion",
							[{approveConclusionTypeCode:"7"},"",""],
							me,me.callBackSubmit,
							"RetailStudentLoanBatchExamUCC");
					dialog.doClose();
				})
			}
			objRoot.prototype.callBackSubmit = function(dwrObject){
			};

			objRoot.prototype.onCancelBatch = function(){
				var me = this;
				var ctrtIdsArray = new Array();
				var selectedNodes = this.mtxApvPkg.getCheckedNodes();
				for(var i = 0;i < selectedNodes.size();i++){
					var recordObj = new Object();
					recordObj.concentrateApproveInfoId = selectedNodes.get(i).getAttribute("jsxid");
					recordObj.virtualApproveObjectId = selectedNodes.get(i).getAttribute("virtualApproveObjectId");
					ctrtIdsArray.push(recordObj);
				}
				ux.megatron.util.Dwr.callDwr("RetailStudentLoanBatchExamUCC",
					"gatherBranchBatchBack",
					[ctrtIdsArray],
					me,me.callBackGatherBranchBatchBack,
					"RetailStudentLoanBatchExamUCC");
			};


			objRoot.prototype.callBackGatherBranchBatchBack = function(dwrObject){
				var cdfDoc = ux.megatron.util.Dwr.toShallowCdf(dwrObject.content.data); 
				if(dwrObject.res.flag == 1){
					//alert("cancel success!");
					this.onRefreshUI();
				}
			};

			objRoot.prototype.onCheckApvPkg = function(){
				if(this.mtxApvPkg.getCheckedNodes().size() == 1 ){
					this.virtualApproveObjectId = this.mtxApvPkg.getCheckedNodes().toArray()[0].getAttribute("virtualApproveObjectId");
					this.approveInfoId = this.mtxApvPkg.getCheckedNodes().toArray()[0].getAttribute("jsxid");
					this.taskId = null;
					if(this.approvalRole == 36){
						this.loadCheckDialog();
					}
					else{
						this.taskId = this.mtxApvPkg.getCheckedNodes().toArray()[0].getAttribute("taskId");
                        console.log(this.taskId);
                        /*
                        ux.megatron.util.Dwr.callDwr("RetailStudentLoanBatchAuditingUCC", "cancelTask", [this.taskId], this, function(dwrObject){
                            console.log(dwrObject);
                        }, "RetailStudentLoanBatchAuditingUCC");
                        */
                        
						var me = this;
                        ux.megatron.util.Dwr.callDwr("RetailStudentLoanBatchAuditingUCC", "isLocked", [this.taskId], this, function(dwrObject){
                            if(!dwrObject.content.data[0]){
                                ux.megatron.util.Dwr.callDwr("RetailStudentLoanBatchAuditingUCC", 
                                    "claimTask", [me.taskId], 
                                    me,me.callBackClaimTask,
                                    "RetailStudentLoanBatchAuditingUCC");
                            } else {
                                var title = ux.megatron.Main.getServer().getDynamicProperty("@cdb@apvPkg@beLockedTitle");
                                var content = ux.megatron.Main.getServer().getDynamicProperty("@cdb@apvPkg@beLockedContent");
                                var ok = ux.megatron.Main.getServer().getDynamicProperty("@cdb@Btn OK");
                                ux.megatron.Dialog.alert(title, content, function(objDlg){objDlg.doClose();}, ok);
                            }
                        }, "RetailStudentLoanBatchAuditingUCC");
                        
					}
				}else{
					alert("Error! Please select a row!");
				}
			};

			objRoot.prototype.callBackClaimTask = function(dwrObject){
				if(dwrObject.res.flag == 1){
					this.loadCheckDialog();
				}else{
					alert("Error! ClaimTask Failed!");
				}
			};

			objRoot.prototype.loadCheckDialog = function(){
				var parent = ux.megatron.Main.getServer().getBodyBlock();
				var me = this;
				ux.megatron.Main.loadUIResourceAsync(parent,
						"ux.cdb.admin",
						"ux/cdb/admin/approvePkg/ui/checkDialog.xml",
						me, me.checkDialogLoaded);
			};

			objRoot.prototype.checkDialogLoaded = function(objEvent){ 
				this.checkDialog = objEvent.obj;
				this.checkDialog.virtualApproveObjectId = this.virtualApproveObjectId;
				this.checkDialog.approveInfoId = this.approveInfoId;
				this.checkDialog.taskId = this.taskId;
				this.checkDialog.opener = this; 
				this.checkDialog.onAfterDeserialize();
			};

			objRoot.prototype.onRefreshUI = function(){
				this.blkApvPkgDesc.show();
				this.setBtn(null,["btnSubmit","btnExam","btnConfirm"]);
				//this.get("selApvStatusFilter").setValue(null);
				this.loadApvPkgData();
			};

			objRoot.prototype.onFilterApvPkg = function(id){
				if(id != 'all'){
					this.mtxApvPkg.filter("mcStatus",this.get("selApvStatusFilter").getText());
				}else{
					this.mtxApvPkg.filter();
				}
			};

			objRoot.prototype.setCommentData = function(msg){
				return;
				if(msg.apvRecord){
					var newCommentRecord = msg.apvRecord;
					var oldCommentRecord = this.commentData.getFirstChild();
					var attrs = newCommentRecord.getAttributeNames().iterator();
					while(attrs.hasNext()){
						var attrName = attrs.next();
						oldCommentRecord.setAttribute(attrName,newCommentRecord.getAttribute(attrName));
					}
				}
				this.get("blkCommentInfo").setData(commentData);
				this.get("blkCommentInfo").repaint();
			};

			objRoot.prototype.setBtn = function(enableBtns,disableBtns,hideBtns){
				if(enableBtns){
					for(var i=0; i<enableBtns.length; i++) {
						this.get(enableBtns[i]).enable();
					}
				}
				if(disableBtns){
					for(var i=0; i<disableBtns.length; i++) {
						this.get(disableBtns[i]).disable();
					}
				}
				if(hideBtns){
					for(var i=0; i<hideBtns.length; i++) {
						this.get(hideBtns[i]).hide();
					}
				}
			};

			objRoot.prototype.onCheckBtnStatus = function(){
				if(this.mtxApvPkg.getCheckedNodes().size() == 0){
					this.setBtn(null,["btnSubmit","btnEdit","btnCancelBatch","btnReview","btnExam","btnConfirm"]);
				}
				else if(this.mtxApvPkg.getCheckedNodes().size() == 1 ){
					this.setBtn(["btnSubmit","btnEdit","btnCancelBatch","btnReview","btnExam","btnConfirm"]);
				}
				else{
					this.setBtn(["btnCancelBatch"],["btnEdit","btnExam","btnReview","btnConfirm"]);
				}
			};

			/**
			* save approve ReceiptPackage conclusions
			*/
			objRoot.prototype.saveApprove = function(approveConclusionInfo){
			
			};

		});
}

